#include "cadretitletab.h"
#include "ui_cadretitletab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include "timeeditdelegate.h"
#include <QDebug>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cdateedit.h"
#include "cadreutil.h"

CadreTitleTab::CadreTitleTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadreTitleTab)
{
    ui->setupUi(this);
}

CadreTitleTab::~CadreTitleTab()
{
    delete ui;
}
void CadreTitleTab::initTitleTableView(QString curPerson)
{
    titleModel = new QSqlTableModel(this);
    titleModel->setTable("Title");
    titleModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter = QString::fromUtf8("IDNum='%1'").arg(curPerson);
    currentPerson=curPerson;
    titleModel->setFilter(filter);
    titleModel->select();
    titleModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("证书号码"));
    titleModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("职称类型"));
    titleModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("职称等级"));
    titleModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("获取时间"));
    titleModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("发证单位"));
    ui->titleTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
    ui->titleTableView->setModel(titleModel);
    TimeEditDelegate *gettime = new TimeEditDelegate();
    ui->titleTableView->setItemDelegateForColumn(4,gettime);
    ui->titleTableView->setColumnHidden(0,true);
    ui->titleTableView->setColumnHidden(6,true);
    ui->titleTableView->setColumnWidth(1,130);
    ui->titleTableView->setColumnWidth(2,130);
    ui->titleTableView->setColumnWidth(3,130);
    ui->titleTableView->setColumnWidth(4,130);
    ui->titleTableView->setColumnWidth(5,150);
    ui->titleTableView->selectRow(titleModel->rowCount()-1);
}
void CadreTitleTab::setButtonEnable(bool i)
{
    ui->titleAddButton->setEnabled(i);
    ui->titleDeleteButton->setEnabled(i);
    ui->titleDownButton->setEnabled(i);
    ui->titleUpButton->setEnabled(i);
    if(i){
        ui->titleTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->titleTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

void CadreTitleTab::on_titleAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加职称"));
    addDialog->setGeometry(700,300,400,380);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);
    QLabel *cnumLabel =new QLabel(trUtf8("证书号码"),addDialog);
    QLabel *typeLabel=new QLabel(trUtf8("职称类型"),addDialog);
    QLabel *levelLabel=new QLabel(trUtf8("职称等级"),addDialog);
    QLabel *gettimeLabel=new QLabel(trUtf8("获取时间"),addDialog);
    QLabel *licenseissuingLabel=new QLabel(trUtf8("发证单位"),addDialog);
    QLineEdit *cnum = new QLineEdit(addDialog);
    QLineEdit *type = new QLineEdit(addDialog);
    QLineEdit *level = new QLineEdit(addDialog);
    CDateEdit *gettime = new CDateEdit(addDialog);
    gettime->setNullable(true);
    gettime->setDisplayFormat("yyyy-MM-dd");
    gettime->setCalendarPopup(true);
    gettime->setDate(QDate());
    QLineEdit *licenseissuing = new QLineEdit(addDialog);

    gridLayout->addWidget(cnumLabel,0,0);
    gridLayout->addWidget(cnum,0,1);
    gridLayout->addWidget(typeLabel,1,0);
    gridLayout->addWidget(type,1,1);
    gridLayout->addWidget(levelLabel,2,0);
    gridLayout->addWidget(level,2,1);
    gridLayout->addWidget(gettimeLabel,3,0);
    gridLayout->addWidget(gettime,3,1);
    gridLayout->addWidget(licenseissuingLabel,4,0);
    gridLayout->addWidget(licenseissuing,4,1);

    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,5,0,1,2,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        QSqlQuery query;
        query.prepare("insert into Title (`CertificateNum`, `TitleType`, `TitleLevel`,"
                      " `TitleGetTime`,`LicenseIssuing`, `IDNum`)"
                      " values(:cnum, :type, :level, :gettime, :licenseissuing, "
                      ":idnum )");
        query.bindValue(":cnum",cnum->text());
        query.bindValue(":type",type->text());
        query.bindValue(":level",level->text());
        query.bindValue(":gettime",gettime->date());
        query.bindValue(":licenseissuing",licenseissuing->text());
        query.bindValue(":idnum",currentPerson);
        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initTitleTableView(currentPerson);
        }else{
            QString info = query.lastError().databaseText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }
    }
}

void CadreTitleTab::on_titleUpButton_clicked()
{
    int cur = CadreUtil::moveUpOne(titleModel,0,ui->titleTableView);
    initTitleTableView(currentPerson);
    ui->titleTableView->selectRow(cur);
}

void CadreTitleTab::on_titleDownButton_clicked()
{
    int cur = CadreUtil::moveDownOne(titleModel,0,ui->titleTableView);
    initTitleTableView(currentPerson);
    ui->titleTableView->selectRow(cur);
}

void CadreTitleTab::on_titleDeleteButton_clicked()
{
    CadreUtil::deleteRow(titleModel, ui->titleTableView);
}
